// Mayank Verma
// Roll No. 2010048
// ARRAY IMPLEMENTATION
#include<stdio.h>
#include<stdlib.h>
int *list;
int size=-1;

// 1. Append
void appending(int num)
        {
        size++;
        list=realloc(list,(size+1)* sizeof(*list));
        list[size]=num;
       
        }
// 2. Sort
        
void sorting()
        {
        int temp,i,j, small, foo;
        i=j=0;
        for(i=0;i<=size;i++)
             {
              small=list[i];
              foo=i;
              for (j=i+1; j<=size; j++)
              {
                  if (list[j]<small)
                  {
                   small=list[j];
                   foo=j;
                  }
              }
              temp=list[i];
              list[i]=list[foo];
              list[foo]=temp;
             }
        
        }

// 3. Reverse
void reverse()
        {
        int i,j, temp;
        i=j=0;      
        if( size%2 == 0 )
         i = size/2 - 1;
        else
         i = size/2;

         for ( j = 0 ; j <= i ; j++ )
         {
          temp = list[j];
          list[j] = list[size -j];
          list[size-j] = temp;
         }

        }



// 4. Index(x) : First Occurence of x
void index_x(int pos)
        {
        if(pos<=size+1)
                {
                printf("\nElement at Position %d is %d",pos,list[pos-1]);
                }
        else
                printf("\nInvalid Position");
        }



// 5. Insert(i, x): Insert x into list at index i
void inserting(int pos)
        {
        int i,num;
        i=num=0;
        if(pos<=size+2)
                {
                printf("\nEnter the number to be inserted: ");
                scanf("%d",&num);
                list=realloc(list,(size+2)*sizeof(*list));      
                for(i=size;i>=(pos-1);i--)
                        list[i+1]=list[i];
                list[pos-1]=num;
                size++;
                }
        else
                printf("\nInvalid Position");
        }


// 6. Count(x): Number of occurences of x in list
void counter(int num)
        {
        int i,count;
        i=count=0;
        for(i=0;i<=size;i++)
                {
                if(list[i]==num)
                        count++;
                }
        printf("\n The Element %d occurs %d times", num, count);
        }


// 7. Remove(x): delete first occurence of x
void removing(int n)
        {
        int i,flag;
        flag=0;

        for(i=0;i<=size;i++)
                {
                if(list[i]==n)
                 {
                  flag=1;
                  for( ; i<size; i++)
                    {
                      list[i]=list[i+1];
                    }                       
                  list=realloc(list,size*sizeof(*list));                  
                  size--;
                  break;
                 }
                }      
        if(flag==0)
                printf("THIS NUMBER IS NOT IN LIST!\n");
        }

// 8. int pop(i): Delete ith element and return it's value
int pop(int pos)
        {
        int i,element;
        if(pos<=size && pos>=0)
                {
                element=list[pos];
                for(i=pos;i<size;i++)
                        list[i]=list[i+1];
                size--;
                list=realloc(list,size*sizeof(*list));
                return element;
                }
        else
                printf("\nInvalid Position");              
        }


// 9. in_list(x): Check if x is in list
void in_list(int n)
{
     int i=0, flag=0;
     for (i=0; i<=size; i++)
      {
       if (list[i]==n)
        flag=1;
      }
     if (flag==1)
      printf("\nElement %d is in the list ", n);
     else
      printf("\nElement %d is not in the list ", n);
}

// 10. DISPLAY
void displaying()
        {
        int j;
        printf("\n");
        for(j=0;j<=size;j++)
                {
                printf("%d -> ",list[j]);
                }
        printf("\n");
        }




void main()
{ int element=0;
  int pos=0;
  int n=0;
  int choice=1;
  int i=0;
  int *list1;
  printf("\n\n\tLIST IMPLEMENTATION THROUGH ARRAY");
  while (choice)
  {
  printf("\n\n 0. EXIT \n 1. APPEND \n 2. SORT \n 3. REVERSE \n 4. INDEX \n 5. INSERT \n 6. COUNT \n 7. REMOVE \n 8. POP \n 9. IN LIST? \n 10. DISPLAY \n Enter Your Choice:\n");
  scanf("%d", &choice);
  switch(choice)
	{
	 case 1: {
		       printf("\nEnter number to append: ");
               scanf("%d", &element);
               appending(element);
               break;
		     }
	 case 2: { sorting();
		       break;
		     }
	 case 3: { reverse();
		      break;
             }
	 case 4: {
              printf("\nEnter Index: ");
              scanf("%d",&pos);
              index_x(pos);
              break;
	      	 }
	 case 5: {
              printf("\nEnter Position");
              scanf(" %d",&pos);
              inserting(pos);
              break;
		 }
	 case 6: {
              printf("\nEnter Integer");
              scanf("%d",&element);
              counter(element);
		      break;
	       	 }
	 case 7: {printf("\nEnter number to be removed");
              scanf("%d",&element);            
              removing(element);
    		  break;
		     }
	 case 8: {
              printf("\nEnter index of element to be popped: ");
              scanf("%d",&pos);
              element=pop(pos-1);
              printf("\nElement %2d Removed", element);
              break;
		     }
	 case 9: {printf("\nEnter number to be checked for: ");
              scanf("%d",&element);
              in_list(element);
		      break;
	      	 }
	 case 10:{ 
		  displaying();
		  break;
		 }
	 default: {
		  break;
		 }
	 	

	}; // switch ends
	displaying();
  }

}
